/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mamayukero.ui.ga;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import mamayukero.ai.ga.GABase;
import mamayukero.problem.environment.Environment;
import mamayukero.problem.environment.map.Const;

/**
 *
 * @author user1
 */
public class UIVisualisasi extends JPanel {
    public JFrame frame;
    public static JTextArea textAsli;
    public static JTextArea textSelect;
    public static JTextArea textCrossOver;
    public static JTextArea textMutation;
    public static JButton butStart;
    public static JButton butStop;
    public JScrollPane jScrollPane1;
    public JScrollPane jScrollPane2;
    public JScrollPane jScrollPane3;
    public JScrollPane jScrollPane4;
    public JTextField textBoxHasil;
    public JTextField textProbCrossOver;
    public JTextField textProbMutasi;
    public JTextField textFitness;
    public JLabel labelJudulAwal;
    public JLabel labelJudulSelect;
    public JLabel labelJudulCrossOver;
    public JLabel labelJudulMutation;
    public JLabel labelProbCrossOver;
    public JLabel labelProbMutation;
    public JLabel labelHasil;
    public JLabel labelFitness;
    public static JLabel labelRandom;
    public JSlider sliderProbMutasi;
    public JSlider sliderProbCrossOver;
    public boolean isStartAwal = true;
    public boolean isStart = false;
    public static GABase ga;
    
    public UIVisualisasi() {
        super();
        ga = new GABase();
        initComponent();
        designSet();
        frame = new JFrame();        
        setLayout(null);
         
        frame.setSize(660,480);
        frame.add(this);
        
        frame.setTitle("GA Visualization");
        frame.setVisible(true);
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        ga.randomAwal();
    }
    
    public void initComponent() {
        
        jScrollPane1 = new JScrollPane();
        jScrollPane1.setBounds(12, 50, 150, 200);
        
        jScrollPane2 = new JScrollPane();
        jScrollPane2.setBounds(168, 50, 150, 200);
        
        jScrollPane3 = new JScrollPane();
        jScrollPane3.setBounds(324, 50, 150, 200);
        
        jScrollPane4 = new JScrollPane();
        jScrollPane4.setBounds(480, 50, 150, 200);
        
        textAsli = new JTextArea();
        //textAsli.setBounds(12,50,150,200);
        //textAsli.setText(ga.printS());
        textAsli.setFont(new Font(Font.DIALOG_INPUT,Font.PLAIN,12));
        textAsli.setEditable(false);
        textAsli.setText(ga.printS());
        jScrollPane1.setViewportView(textAsli);
        this.add(jScrollPane1);
        //this.add(textAsli);
        
        textSelect = new JTextArea();
        //textAsli.setBounds(12,50,150,200);
        textSelect.setFont(new Font(Font.DIALOG_INPUT,Font.PLAIN,12));
        textSelect.setEditable(false);
        jScrollPane2.setViewportView(textSelect);
        this.add(jScrollPane2);
        
        textCrossOver = new JTextArea();
        //textAsli.setBounds(12,50,150,200);
        //textAsli.setText(ga.printS());
        textCrossOver.setFont(new Font(Font.DIALOG_INPUT,Font.PLAIN,12));
        textCrossOver.setEditable(false);
        jScrollPane3.setViewportView(textCrossOver);
        this.add(jScrollPane3);
        
        textMutation = new JTextArea();
        //textAsli.setBounds(12,50,150,200);
        //textAsli.setText(ga.printS());
        textMutation.setFont(new Font(Font.DIALOG_INPUT,Font.PLAIN,12));
        textMutation.setEditable(false);
        jScrollPane4.setViewportView(textMutation);
        this.add(jScrollPane4);
        
        
        
        labelJudulAwal = new JLabel("Awal");
        labelJudulAwal.setBounds(12, 25, 150, 30);
        this.add(labelJudulAwal);
        
        labelJudulSelect = new JLabel("Seleksi");
        labelJudulSelect.setBounds(162, 25, 150, 30);
        this.add(labelJudulSelect);
        
        labelJudulCrossOver = new JLabel("Cross Over");
        labelJudulCrossOver.setBounds(324, 25, 150, 30);
        this.add(labelJudulCrossOver);
        
        labelJudulMutation = new JLabel("Mutasi");
        labelJudulMutation.setBounds(480, 25, 150, 30);
        this.add(labelJudulMutation);
        
        labelProbCrossOver = new JLabel("% Cross Over = ");
        labelProbCrossOver.setBounds(12, 262, 150, 30);
        this.add(labelProbCrossOver);
        
        labelProbMutation = new JLabel("% Mutasi = ");
        labelProbMutation.setBounds(324, 262, 150, 30);
        this.add(labelProbMutation);
      
        textProbCrossOver = new JTextField();
        textProbCrossOver.setBounds(162, 262, 40, 30);
        textProbCrossOver.setEditable(false);
        this.add(textProbCrossOver);
        
        sliderProbCrossOver = new JSlider(0,100,GABase.pCross);
        sliderProbCrossOver.setBounds(5, 294, 315, 50);
        sliderProbCrossOver.setMinorTickSpacing(1);
        sliderProbCrossOver.setMajorTickSpacing(10);
        sliderProbCrossOver.setPaintTicks(true);
        sliderProbCrossOver.setPaintLabels(true);
        sliderProbCrossOver.addChangeListener(new ChangeListener() {

            @Override
            public void stateChanged(ChangeEvent e) {
                GABase.pCross = sliderProbCrossOver.getValue();
                textProbCrossOver.setText(""+sliderProbCrossOver.getValue());
            }
        });
        this.add(sliderProbCrossOver);
        
        labelHasil = new JLabel("Hasil GA");
        labelHasil.setBounds(12, sliderProbCrossOver.getBounds().y+sliderProbCrossOver.getBounds().height+10, 100, 30);
        this.add(labelHasil);
        
        textProbCrossOver.setText(""+sliderProbCrossOver.getValue());
        
        textProbMutasi = new JTextField();
        textProbMutasi.setBounds(480, 262, 40, 30);
        textProbMutasi.setEditable(false);
        this.add(textProbMutasi);
        
        
        sliderProbMutasi = new JSlider(0,100,GABase.pMutation);
        //sliderProbMutasi.setBounds(324, 294, 312, 50);
        sliderProbMutasi.setBounds(sliderProbCrossOver.getBounds().x+sliderProbCrossOver.getBounds().width+10, 294, 315, 50);
        
        sliderProbMutasi.setMinorTickSpacing(1);
        sliderProbMutasi.setMajorTickSpacing(10);
        sliderProbMutasi.setPaintTicks(true);
        sliderProbMutasi.setPaintLabels(true);
        sliderProbMutasi.addChangeListener(new ChangeListener() {

            @Override
            public void stateChanged(ChangeEvent e) {
                GABase.pMutation = sliderProbMutasi.getValue();
                textProbMutasi.setText(""+sliderProbMutasi.getValue());
            }
        });
        this.add(sliderProbMutasi);
        textProbMutasi.setText(""+sliderProbMutasi.getValue());
        
        butStart = new JButton();
        butStart.setText("START");
        butStart.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                if (!isStart) {
                    if (!isStartAwal) {
                        ga = new GABase();
                        ga.randomAwal();
                    } else
                        isStartAwal = false;
                    sliderProbCrossOver.setEnabled(false);
                    sliderProbMutasi.setEnabled(false);
                    UIVisualisasi.start();
                    isStart = true;
                }
            }
        });
        this.add(butStart);
        butStart.setBounds(385, labelHasil.getBounds().y, 100, 30);
        
        butStop = new JButton();
        butStop.setText("STOP");
        butStop.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                GABase.isLoop = false;
                textBoxHasil.setText(GABase.maxAllele);
                textFitness.setText(""+GABase.maxFitness);
                sliderProbCrossOver.setEnabled(true);
                sliderProbMutasi.setEnabled(true);
                Environment.initWorkflow(GABase.maxAllele);
                //System.out.println(Environment.map.truck.pickupSchedule);
                //GABase.maxFitness = 0;
                //GABase.maxAllele = "";
                //isStart = false;
            }
        });
        this.add(butStop);
        butStop.setBounds(butStart.getBounds().x+butStart.getWidth()+10, butStart.getBounds().y, 100, 30);
        
        labelFitness = new JLabel("Hasil Fitness = ");
        labelFitness.setBounds(12, labelHasil.getBounds().y+labelHasil.getHeight()+5, 100, 30);
        this.add(labelFitness);
        
        textBoxHasil = new JTextField();
        textBoxHasil.setBounds(labelHasil.getBounds().x+labelHasil.getWidth()+10,labelHasil.getBounds().y,150,30);
        textBoxHasil.setEditable(false);
        this.add(textBoxHasil);
        
        textFitness = new JTextField();
        textFitness.setBounds(labelFitness.getBounds().x+labelFitness.getWidth()+10, labelFitness.getBounds().y, 150, 30);
        textFitness.setEditable(false);
        this.add(textFitness);
        
        labelRandom = new JLabel("Randomizing...");
        labelRandom.setBounds(324, butStop.getBounds().y + 32, 150, 30);
        this.add(labelRandom);
    }
    
    public static void main (String args[]) {
        Environment.Reset();
        Environment.load(Const.FILE_INPUT);
        UIVisualisasi uiVisualisasi = new UIVisualisasi();
    }
    
    public static void start() {
        new Thread(new Runnable() {
            
            @Override
            public void run() {
                GABase.isLoop = true;
                while (GABase.isLoop) {
                    textAsli.setText(ga.printS());
                    ga.Select();
                    textSelect.setText(ga.printS());
                    ga.CrossOver();
                    textCrossOver.setText(ga.printS());
                    ga.Mutate();
                    textMutation.setText(ga.printS());
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(UIVisualisasi.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        }).start();
    }
    
    public void designSet()
    {
        this.setBackground(new java.awt.Color(217,189,143));      
      /*  labelJudulAwal.setForeground(new java.awt.Color(231,230,255));
        labelJudulCrossOver.setForeground(new java.awt.Color(231,230,255));        
        labelJudulMutation.setForeground(new java.awt.Color(231,230,255));
        labelJudulSelect.setForeground(new java.awt.Color(231,230,255));
       */ labelJudulAwal.setFont(new java.awt.Font("Segoe UI", 1, 13));
        labelJudulCrossOver.setFont(new java.awt.Font("Segoe UI", 1, 13));
        labelJudulMutation.setFont(new java.awt.Font("Segoe UI", 1, 13));
        labelJudulSelect.setFont(new java.awt.Font("Segoe UI", 1, 13));
        labelFitness.setFont(new java.awt.Font("Segoe UI", 1, 13));
        labelHasil.setFont(new java.awt.Font("Segoe UI", 1, 13));
        labelProbMutation.setFont(new java.awt.Font("Segoe UI", 1, 13));
        labelProbCrossOver.setFont(new java.awt.Font("Segoe UI", 1, 13));
        textAsli.setBackground(new java.awt.Color(245,240,171));   
        textBoxHasil.setBackground(new java.awt.Color(245,240,171));
        textCrossOver.setBackground(new java.awt.Color(245,240,171));
        textFitness.setBackground(new java.awt.Color(245,240,171));
        textMutation.setBackground(new java.awt.Color(245,240,171));
        textProbCrossOver.setBackground(new java.awt.Color(245,240,171));
        textProbMutasi.setBackground(new java.awt.Color(245,240,171));
        textSelect.setBackground(new java.awt.Color(245,240,171));
        sliderProbCrossOver.setBackground(new java.awt.Color(88, 138, 127));
        sliderProbMutasi.setBackground(new java.awt.Color(88, 138, 127));
        sliderProbCrossOver.setForeground(new java.awt.Color(231,230,255));
        sliderProbMutasi.setForeground(new java.awt.Color(231,230,255));
        butStart.setBackground(new java.awt.Color(217,176,102));
        butStart.setForeground(new java.awt.Color(231,255,255));
        butStart.setFont(new java.awt.Font("Segoe UI", 1, 13));
        butStop.setBackground(new java.awt.Color(217,176,102));
        butStop.setForeground(new java.awt.Color(231,255,255));
        butStop.setFont(new java.awt.Font("Segoe UI", 1, 13));
        
    }
}
